
#version 410 core
struct Material
{
	// 漫反射贴图
	sampler2D   diffuse;
	// 镜面光照的颜色
	sampler2D  specular;
	// 反光度
	// 越大  亮点越小 光束越集中
	float shininess;
};

struct Light
{
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};

// 定向光源
struct DirectionLight
{
	vec3 direction;
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};

out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoords;
uniform vec3 viewPos;
uniform Material material;
uniform DirectionLight light;

vec3 calcDirLight(DirectionLight light, Material material, vec3 normal, vec3 viewDir, vec2 texCoords)
{
	vec3 lightDir = normalize(-light.direction);
	// 漫反射着色
	float diff = max(dot(normal, lightDir), 0.0);
	// 镜面光着色
	vec3 reflectDir = reflect(-lightDir, normal);
	float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
	// 合并结果
	vec3 ambient  = light.ambient  * vec3(texture(material.diffuse, texCoords));
	vec3 diffuse  = light.diffuse  * diff * vec3(texture(material.diffuse, texCoords));
	vec3 specular = light.specular * spec * vec3(texture(material.specular, texCoords));
	return (ambient + diffuse + specular);
}

void main()
{
	vec3 norm = normalize(Normal);
	vec3 viewDir = normalize(viewPos - FragPos);
	vec3 result = calcDirLight(light, material, norm, viewDir, TexCoords);
	FragColor = vec4(result, 1.0);
}